In [ ]:
# 1. Install libraries
In [8]:
!pip install pandas
!pip install requests
!pip install bs4
!pip install html5lib 
!pip install lxml
!pip install plotly
!pip install nbformat
!pip install yfinance
Requirement already satisfied: pandas in /opt/conda/lib/python3.12/site-packages (2.2.3)
Requirement already satisfied: numpy>=1.26.0 in /opt/conda/lib/python3.12/site-packages (from pandas) (2.2.4)
Requirement already satisfied: python-dateutil>=2.8.2 in /opt/conda/lib/python3.12/site-packages (from pandas) (2.9.0.post0)
Requirement already satisfied: pytz>=2020.1 in /opt/conda/lib/python3.12/site-packages (from pandas) (2024.2)
Requirement already satisfied: tzdata>=2022.7 in /opt/conda/lib/python3.12/site-packages (from pandas) (2025.2)
Requirement already satisfied: six>=1.5 in /opt/conda/lib/python3.12/site-packages (from python-dateutil>=2.8.2->pandas) (1.17.0)
Requirement already satisfied: requests in /opt/conda/lib/python3.12/site-packages (2.32.3)
Requirement already satisfied: charset_normalizer<4,>=2 in /opt/conda/lib/python3.12/site-packages (from requests) (3.4.1)
Requirement already satisfied: idna<4,>=2.5 in /opt/conda/lib/python3.12/site-packages (from requests) (3.10)
Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/conda/lib/python3.12/site-packages (from requests) (2.3.0)
Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/lib/python3.12/site-packages (from requests) (2024.12.14)
Requirement already satisfied: bs4 in /opt/conda/lib/python3.12/site-packages (0.0.2)
Requirement already satisfied: beautifulsoup4 in /opt/conda/lib/python3.12/site-packages (from bs4) (4.12.3)
Requirement already satisfied: soupsieve>1.2 in /opt/conda/lib/python3.12/site-packages (from beautifulsoup4->bs4) (2.5)
Requirement already satisfied: html5lib in /opt/conda/lib/python3.12/site-packages (1.1)
Requirement already satisfied: six>=1.9 in /opt/conda/lib/python3.12/site-packages (from html5lib) (1.17.0)
Requirement already satisfied: webencodings in /opt/conda/lib/python3.12/site-packages (from html5lib) (0.5.1)
Requirement already satisfied: lxml in /opt/conda/lib/python3.12/site-packages (5.3.2)
Requirement already satisfied: plotly in /opt/conda/lib/python3.12/site-packages (5.24.1)
Requirement already satisfied: tenacity>=6.2.0 in /opt/conda/lib/python3.12/site-packages (from plotly) (9.0.0)
Requirement already satisfied: packaging in /opt/conda/lib/python3.12/site-packages (from plotly) (24.2)
Requirement already satisfied: nbformat in /opt/conda/lib/python3.12/site-packages (5.10.4)
Requirement already satisfied: fastjsonschema>=2.15 in /opt/conda/lib/python3.12/site-packages (from nbformat) (2.21.1)
Requirement already satisfied: jsonschema>=2.6 in /opt/conda/lib/python3.12/site-packages (from nbformat) (4.23.0)
Requirement already satisfied: jupyter-core!=5.0.*,>=4.12 in /opt/conda/lib/python3.12/site-packages (from nbformat) (5.7.2)
Requirement already satisfied: traitlets>=5.1 in /opt/conda/lib/python3.12/site-packages (from nbformat) (5.14.3)
Requirement already satisfied: attrs>=22.2.0 in /opt/conda/lib/python3.12/site-packages (from jsonschema>=2.6->nbformat) (25.1.0)
Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /opt/conda/lib/python3.12/site-packages (from jsonschema>=2.6->nbformat) (2024.10.1)
Requirement already satisfied: referencing>=0.28.4 in /opt/conda/lib/python3.12/site-packages (from jsonschema>=2.6->nbformat) (0.36.2)
Requirement already satisfied: rpds-py>=0.7.1 in /opt/conda/lib/python3.12/site-packages (from jsonschema>=2.6->nbformat) (0.22.3)
Requirement already satisfied: platformdirs>=2.5 in /opt/conda/lib/python3.12/site-packages (from jupyter-core!=5.0.*,>=4.12->nbformat) (4.3.6)
Requirement already satisfied: typing-extensions>=4.4.0 in /opt/conda/lib/python3.12/site-packages (from referencing>=0.28.4->jsonschema>=2.6->nbformat) (4.12.2)
Collecting yfinance
  Downloading yfinance-0.2.55-py2.py3-none-any.whl.metadata (5.8 kB)
Requirement already satisfied: pandas>=1.3.0 in /opt/conda/lib/python3.12/site-packages (from yfinance) (2.2.3)
Requirement already satisfied: numpy>=1.16.5 in /opt/conda/lib/python3.12/site-packages (from yfinance) (2.2.4)
Requirement already satisfied: requests>=2.31 in /opt/conda/lib/python3.12/site-packages (from yfinance) (2.32.3)
Collecting multitasking>=0.0.7 (from yfinance)
  Downloading multitasking-0.0.11-py3-none-any.whl.metadata (5.5 kB)
Requirement already satisfied: platformdirs>=2.0.0 in /opt/conda/lib/python3.12/site-packages (from yfinance) (4.3.6)
Requirement already satisfied: pytz>=2022.5 in /opt/conda/lib/python3.12/site-packages (from yfinance) (2024.2)
Requirement already satisfied: frozendict>=2.3.4 in /opt/conda/lib/python3.12/site-packages (from yfinance) (2.4.6)
Collecting peewee>=3.16.2 (from yfinance)
  Downloading peewee-3.17.9.tar.gz (3.0 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.0/3.0 MB 77.0 MB/s eta 0:00:00
  Installing build dependencies ... one
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Requirement already satisfied: beautifulsoup4>=4.11.1 in /opt/conda/lib/python3.12/site-packages (from yfinance) (4.12.3)
Requirement already satisfied: soupsieve>1.2 in /opt/conda/lib/python3.12/site-packages (from beautifulsoup4>=4.11.1->yfinance) (2.5)
Requirement already satisfied: python-dateutil>=2.8.2 in /opt/conda/lib/python3.12/site-packages (from pandas>=1.3.0->yfinance) (2.9.0.post0)
Requirement already satisfied: tzdata>=2022.7 in /opt/conda/lib/python3.12/site-packages (from pandas>=1.3.0->yfinance) (2025.2)
Requirement already satisfied: charset_normalizer<4,>=2 in /opt/conda/lib/python3.12/site-packages (from requests>=2.31->yfinance) (3.4.1)
Requirement already satisfied: idna<4,>=2.5 in /opt/conda/lib/python3.12/site-packages (from requests>=2.31->yfinance) (3.10)
Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/conda/lib/python3.12/site-packages (from requests>=2.31->yfinance) (2.3.0)
Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/lib/python3.12/site-packages (from requests>=2.31->yfinance) (2024.12.14)
Requirement already satisfied: six>=1.5 in /opt/conda/lib/python3.12/site-packages (from python-dateutil>=2.8.2->pandas>=1.3.0->yfinance) (1.17.0)
Downloading yfinance-0.2.55-py2.py3-none-any.whl (109 kB)
Downloading multitasking-0.0.11-py3-none-any.whl (8.5 kB)
Building wheels for collected packages: peewee
  Building wheel for peewee (pyproject.toml) ... one
  Created wheel for peewee: filename=peewee-3.17.9-cp312-cp312-linux_x86_64.whl size=303832 sha256=c03e0f1b42ec67b5244d2ab7dca4f04ee624b3d0e469989cdef74cf455ca2460
  Stored in directory: /home/jupyterlab/.cache/pip/wheels/43/ef/2d/2c51d496bf084945ffdf838b4cc8767b8ba1cc20eb41588831
Successfully built peewee
Installing collected packages: peewee, multitasking, yfinance
Successfully installed multitasking-0.0.11 peewee-3.17.9 yfinance-0.2.55
In [1]:
# 2. Import libraries
In [10]:
import pandas as pd
import yfinance as yf
import requests as req
import plotly.io as pio
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from bs4 import BeautifulSoup
from IPython.display import display, HTML
In [3]:
# 3. Download Tesla stock data
In [11]:
yf_tesla = yf.Ticker('TSLA')
tesla_data = yf_tesla.history(period = 'max')
tesla_data.reset_index(inplace = True)
print(f'Tesla Info\n{tesla_data.head().to_string()}\n')
Tesla Info
                       Date      Open      High       Low     Close     Volume  Dividends  Stock Splits
0 2010-06-29 00:00:00-04:00  1.266667  1.666667  1.169333  1.592667  281494500        0.0           0.0
1 2010-06-30 00:00:00-04:00  1.719333  2.028000  1.553333  1.588667  257806500        0.0           0.0
2 2010-07-01 00:00:00-04:00  1.666667  1.728000  1.351333  1.464000  123282000        0.0           0.0
3 2010-07-02 00:00:00-04:00  1.533333  1.540000  1.247333  1.280000   77097000        0.0           0.0
4 2010-07-06 00:00:00-04:00  1.333333  1.333333  1.055333  1.074000  103003500        0.0           0.0

In [ ]:
# 4. Scrape Tesla revenue data (second table)
In [12]:
url = 'https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/revenue.htm'
data = req.get(url).text
root = BeautifulSoup(data, 'html.parser')
tesla = pd.DataFrame(columns = ['Date', 'Revenue'])
tables = root.find_all('table')
tbody = tables[1].find('tbody')
for row in tbody.find_all('tr'):
    col = row.find_all('td')
    date = col[0].text
    revenue = col[1].text
    tesla = pd.concat([tesla, pd.DataFrame({'Date' : [date], 'Revenue' : [revenue]})], ignore_index = True)
tesla["Revenue"] = tesla['Revenue'].str.replace(',|\$',"", regex = True)
tesla.dropna(inplace = True)
tesla = tesla[tesla['Revenue'] != ""]
print(f'Tesla Revenue\n{tesla.tail()}')
Tesla Revenue
          Date Revenue
48  2010-09-30      31
49  2010-06-30      28
50  2010-03-31      21
52  2009-09-30      46
53  2009-06-30      27
In [ ]:
# 5. Download GameStop stock data and revenue
In [13]:
yf_gme = yf.Ticker('GME')
gme_data = yf_gme.history(period = 'max')
gme_data.reset_index(inplace = True)
print(f'GameStop Info\n{gme_data.head().to_string()}\n')
url = 'https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/stock.html'
data = req.get(url).text
root = BeautifulSoup(data, 'html.parser')
gme = pd.DataFrame(columns = ['Date', 'Revenue'])
tables = root.find_all('table')
tbody = tables[1].find('tbody')
for row in tbody.find_all('tr'):
    col = row.find_all('td')
    date = col[0].text
    revenue = col[1].text{"nbformat": 4, "nbformat_minor": 5, "metadata": {"language": "python"}, "cells": [{"id": "6a3741ab", "cell_type": "code", "metadata": {}, "execution_count": null, "source": "import pandas as pd\nimport yfinance as yf\nimport requests as req\nimport plotly.io as pio\nimport plotly.graph_objects as go\nfrom plotly.subplots import make_subplots\nfrom bs4 import BeautifulSoup\nfrom IPython.display import display, HTML", "outputs": []}, {"id": "5fe1e75f", "cell_type": "code", "metadata": {}, "execution_count": null, "source": "yf_tesla = yf.Ticker('TSLA')\ntesla_data = yf_tesla.history(period = 'max')\ntesla_data.reset_index(inplace = True)\nprint(f'Tesla Info\\n{tesla_data.head().to_string()}\\n')", "outputs": []}, {"id": "92e647ef", "cell_type": "code", "metadata": {}, "execution_count": null, "source": "url = 'https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/revenue.htm'\ndata = req.get(url).text\nroot = BeautifulSoup(data, 'html.parser')\ntesla = pd.DataFrame(columns = ['Date', 'Revenue'])\ntables = root.find_all('table')\ntbody = tables[1].find('tbody')\nfor row in tbody.find_all('tr'):\n    col = row.find_all('td')\n    date = col[0].text\n    revenue = col[1].text\n    tesla = pd.concat([tesla, pd.DataFrame({'Date' : [date], 'Revenue' : [revenue]})], ignore_index = True)\ntesla[\"Revenue\"] = tesla['Revenue'].str.replace(',|\\$',\"\", regex = True)\ntesla.dropna(inplace = True)\ntesla = tesla[tesla['Revenue'] != \"\"]\nprint(f'Tesla Revenue\\n{tesla.tail()}')", "outputs": []}, {"id": "05fa74a7", "cell_type": "code", "metadata": {}, "execution_count": null, "source": "yf_gme = yf.Ticker('GME')\ngme_data = yf_gme.history(period = 'max')\ngme_data.reset_index(inplace = True)\nprint(f'GameStop Info\\n{gme_data.head().to_string()}\\n')", "outputs": []}, {"id": "9a069595", "cell_type": "code", "metadata": {}, "execution_count": null, "source": "url = 'https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/stock.html'\ndata = req.get(url).text\nroot = BeautifulSoup(data, 'html.parser')\ngme = pd.DataFrame(columns = ['Date', 'Revenue'])\ntables = root.find_all('table')\ntbody = tables[1].find('tbody')\nfor row in tbody.find_all('tr'):\n    col = row.find_all('td')\n    date = col[0].text\n    revenue = col[1].text\n    gme = pd.concat([gme, pd.DataFrame({'Date' : [date], 'Revenue' : [revenue]})], ignore_index = True)\ngme[\"Revenue\"] = gme['Revenue'].str.replace(',|\\$',\"\", regex = True)\ngme.dropna(inplace = True)\ngme = gme[gme['Revenue'] != \"\"]\nprint(f'GameStop\\n{gme.tail()}')", "outputs": []}, {"id": "4d5e5d7f", "cell_type": "code", "metadata": {}, "execution_count": null, "source": "pio.renderers.default = \"iframe\"\ndef make_graph(stock_data, revenue_data, stock):\n    fig = make_subplots(rows = 2, cols = 1, shared_xaxes = True, subplot_titles = (\"Historical Share Price\", \"Historical Revenue\"), vertical_spacing = .3)\n    stock_data_specific = stock_data[stock_data.Date <= '2021-06-14']\n    revenue_data_specific = revenue_data[revenue_data.Date <= '2021-04-30']\n    fig.add_trace(go.Scatter(x = pd.to_datetime(stock_data_specific.Date), y = stock_data_specific.Close.astype(\"float\"), name = \"Share Price\"), row = 1, col = 1)\n    fig.add_trace(go.Scatter(x = pd.to_datetime(revenue_data_specific.Date), y = revenue_data_specific.Revenue.astype(\"float\"), name = \"Revenue\"), row = 2, col = 1)\n    fig.update_xaxes(title_text=\"Date\", row=1, col=1)\n    fig.update_xaxes(title_text=\"Date\", row=2, col=1)\n    fig.update_yaxes(title_text=\"Price ($US)\", row=1, col=1)\n    fig.update_yaxes(title_text=\"Revenue ($US Millions)\", row=2, col=1)\n    fig.update_layout(\n        showlegend = False,\n        height = 900,\n        title = stock,\n        xaxis_rangeslider_visible = True\n    )\n    fig_html = fig.to_html()\n    display(HTML(fig_html))", "outputs": []}, {"id": "91301525", "cell_type": "code", "metadata": {}, "execution_count": null, "source": "make_graph(tesla_data, tesla, 'Tesla')\nmake_graph(gme_data, gme, 'GameStop')", "outputs": []}]}
    gme = pd.concat([gme, pd.DataFrame({'Date' : [date], 'Revenue' : [revenue]})], ignore_index = True)
gme["Revenue"] = gme['Revenue'].str.replace(',|\$',"", regex = True)
gme.dropna(inplace = True)
gme = gme[gme['Revenue'] != ""]
print(f'GameStop\n{gme.tail()}')
GameStop Info
                       Date      Open      High       Low     Close    Volume  Dividends  Stock Splits
0 2002-02-13 00:00:00-05:00  1.620129  1.693350  1.603296  1.691667  76216000        0.0           0.0
1 2002-02-14 00:00:00-05:00  1.712707  1.716073  1.670626  1.683250  11021600        0.0           0.0
2 2002-02-15 00:00:00-05:00  1.683250  1.687458  1.658001  1.674834   8389600        0.0           0.0
3 2002-02-19 00:00:00-05:00  1.666418  1.666418  1.578047  1.607504   7410400        0.0           0.0
4 2002-02-20 00:00:00-05:00  1.615920  1.662210  1.603296  1.662210   6892800        0.0           0.0

GameStop
          Date Revenue
57  2006-01-31    1667
58  2005-10-31     534
59  2005-07-31     416
60  2005-04-30     475
61  2005-01-31     709
In [ ]:
# 6. Define a plotting function and visualize
In [14]:
pio.renderers.default = "iframe"
def make_graph(stock_data, revenue_data, stock):
    fig = make_subplots(rows = 2, cols = 1, shared_xaxes = True, subplot_titles = ("Historical Share Price", "Historical Revenue"), vertical_spacing = .3)
    stock_data_specific = stock_data[stock_data.Date <= '2021-06-14']
    revenue_data_specific = revenue_data[revenue_data.Date <= '2021-04-30']
    fig.add_trace(go.Scatter(x = pd.to_datetime(stock_data_specific.Date), y = stock_data_specific.Close.astype("float"), name = "Share Price"), row = 1, col = 1)
    fig.add_trace(go.Scatter(x = pd.to_datetime(revenue_data_specific.Date), y = revenue_data_specific.Revenue.astype("float"), name = "Revenue"), row = 2, col = 1)
    fig.update_xaxes(title_text="Date", row=1, col=1)
    fig.update_xaxes(title_text="Date", row=2, col=1)
    fig.update_yaxes(title_text="Price ($US)", row=1, col=1)
    fig.update_yaxes(title_text="Revenue ($US Millions)", row=2, col=1)
    fig.update_layout(
    showlegend = False,
    height = 900,
    title = stock,
    xaxis_rangeslider_visible = True
    )
    fig_html = fig.to_html()
    display(HTML(fig_html))

make_graph(tesla_data, tesla, 'Tesla')
make_graph(gme_data, gme, 'GameStop')